"""update user

Revision ID: d7f534c56b43
Revises: 43c42a1daf5f
Create Date: 2025-01-20 14:41:48.982899

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel

# revision identifiers, used by Alembic.
revision: str = 'd7f534c56b43'
down_revision: Union[str, None] = '43c42a1daf5f'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # 首先创建枚举类型
    op.execute("CREATE TYPE accounttype AS ENUM ('PHONE', 'EMAIL', 'WECHAT', 'QQ', 'WEIBO')")
    
    # 然后添加列
    op.add_column('users', sa.Column('account', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False, comment='用户主账号(手机号/邮箱/第三方账号ID)'))
    op.add_column('users', sa.Column('accountType', sa.Enum('PHONE', 'EMAIL', 'WECHAT', 'QQ', 'WEIBO', name='accounttype', create_type=False), server_default=sa.text("'PHONE'"), nullable=False))
    op.create_index(op.f('ix_users_account'), 'users', ['account'], unique=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    # 首先删除使用该枚举类型的列和索引
    op.drop_index(op.f('ix_users_account'), table_name='users')
    op.drop_column('users', 'accountType')
    op.drop_column('users', 'account')
    
    # 最后删除枚举类型
    op.execute("DROP TYPE accounttype")
    # ### end Alembic commands ###
